
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.Sys;
using Apps.Models.Sys;
using Apps.IBLL.Sys;
namespace Apps.BLL.Sys
{
	public partial class SysSampleBLL: Virtual_SysSampleBLL,ISysSampleBLL
	{
        

	}
	public class Virtual_SysSampleBLL
	{
        [Dependency]
        public ISysSampleRepository m_Rep { get; set; }

		public virtual List<SysSampleModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<SysSample> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.Id.Contains(queryStr)
								|| a.Name.Contains(queryStr)
								
								
								|| a.Photo.Contains(queryStr)
								|| a.Note.Contains(queryStr)
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<SysSampleModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<SysSampleModel>();
		}

        public virtual List<SysSampleModel> CreateModelList(ref IQueryable<SysSample> queryData)
        {

            List<SysSampleModel> modelList = (from r in queryData
                                              select new SysSampleModel
                                              {
													Id = r.Id,
													Name = r.Name,
													Age = r.Age,
													Bir = r.Bir,
													Photo = r.Photo,
													Note = r.Note,
													CreateTime = r.CreateTime,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, SysSampleModel model)
        {
            try
            {
                SysSample entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new SysSample();
               				entity.Id = model.Id;
				entity.Name = model.Name;
				entity.Age = model.Age;
				entity.Bir = model.Bir;
				entity.Photo = model.Photo;
				entity.Note = model.Note;
				entity.CreateTime = model.CreateTime;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, SysSampleModel model)
        {
            try
            {
                SysSample entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.Id = model.Id;
				entity.Name = model.Name;
				entity.Age = model.Age;
				entity.Bir = model.Bir;
				entity.Photo = model.Photo;
				entity.Note = model.Note;
				entity.CreateTime = model.CreateTime;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual SysSampleModel GetById(object id)
        {
            if (IsExists(id))
            {
                SysSample entity = m_Rep.GetById(id);
                SysSampleModel model = new SysSampleModel();
                              				model.Id = entity.Id;
				model.Name = entity.Name;
				model.Age = entity.Age;
				model.Bir = entity.Bir;
				model.Photo = entity.Photo;
				model.Note = entity.Note;
				model.CreateTime = entity.CreateTime;
 
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<SysSampleModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<SysSampleModel>(x => x.Name, "Name");
				 excelFile.AddMapping<SysSampleModel>(x => x.Age, "Age");
				 excelFile.AddMapping<SysSampleModel>(x => x.Bir, "Bir");
				 excelFile.AddMapping<SysSampleModel>(x => x.Photo, "Photo");
				 excelFile.AddMapping<SysSampleModel>(x => x.Note, "Note");
				 excelFile.AddMapping<SysSampleModel>(x => x.CreateTime, "CreateTime");
 
            //SheetName
            var excelContent = excelFile.Worksheet<SysSampleModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new SysSampleModel();
						 				  entity.Id = row.Id;
				  entity.Name = row.Name;
				  entity.Age = row.Age;
				  entity.Bir = row.Bir;
				  entity.Photo = row.Photo;
				  entity.Note = row.Note;
				  entity.CreateTime = row.CreateTime;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<SysSampleModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        SysSample entity = new SysSample();
                       						entity.Id = ResultHelper.NewId;
						entity.Name = model.Name;
						entity.Age = model.Age;
						entity.Bir = model.Bir;
						entity.Photo = model.Photo;
						entity.Note = model.Note;
						entity.CreateTime = ResultHelper.NowTime;
 
                        db.SysSample.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
